<?php

namespace backend\modules\openapi\controllers;

use backend\models\OpenapiShop;
use common\models\SysShop;
use common\models\SysUser;
use common\models\BusServiceOrder;
use common\models\BusMemberConsume;
use yii\web\Controller;
use common\models\SysBoss;

class DefaultController extends Controller
{
    public $enableCsrfValidation = false;

    public function init()
    {
        // 验证签名
        $timestamp = isset($_POST['timestamp']) ? $_POST['timestamp'] : '';
        $nonce = isset($_POST['nonce']) ? $_POST['nonce'] : '';
        $sign = isset($_POST['sign']) ? $_POST['sign'] : '';
        if (empty($timestamp) || empty($nonce)) {
            echo json_encode(array('status' => 1000, 'result' => 'error'));
            exit;
        }
        $model = new OpenapiShop();
        $create_sign = $model->create_sign($timestamp, $nonce); // 生成签名
        if ($sign != $create_sign) {
            echo json_encode(array('status' => 1000, 'result' => 'error'));
            exit;
        }
    }

    /***
     * 接口数据 通过手机获取身份
     */
    public function actionGetboss()
    {
        $login_name = isset($_POST['mobile']) ? $_POST['mobile'] : '';
        $type_name = isset($_POST['type']) ? $_POST['type'] : '';
        if (empty($login_name)) {
            echo json_encode(array('status' => 1000, 'result' => 'error'));
            exit;
        }

        $user = SysUser::find()->where("login_name = :login_name", [':login_name' => $login_name])->asArray()->all();
        if(empty($user)){
            echo json_encode(array('status' => 1000, 'result' => '用户不存在'));
            exit;
        }
        $type = $user[0]['user_type'];
        switch ($type) {
            case 1;
                $name = 1;
                break;
            case 2;
                $name = 2;
                break;
            case 3;
                $name = 3;
                break;
            case 4;
                $name = 4;
                break;
        }
        if (empty($type_name)) {
            echo json_encode(array('status' => 10000, 'result' => array('type' => $name)));
        } else {
            if ($type == 3) {//老板
                $shop = SysShop::find()->where("boss_uid = :boss_uid", [':boss_uid' => $user[0]['uid']])->asArray()->all();
                $arr = [];
                foreach ($shop as $k => $v){
                    $arr[$k]['shop_id'] = $v['shop_id'];
                    $arr[$k]['shop_name'] = $v['shop_name'];
                }
                return json_encode(array('status' => 10000, 'result' => $arr));
            }
            if ($name == 4) {//店长
                $shop = SysShop::find()->where("shop_uid = :shop_uid", [':shop_uid' => $user[0]['uid']])->asArray()->one();
                $arr = [
                    'shop_id' => $shop['shop_id'],
                    'shop_name' => $shop['shop_name'],
                ];
                return json_encode(array('status' => 10000, 'result' => $arr));
            }
        }
        
    }

    
    /**
     * 统计今日(本月)到店人数、消耗金额、现金、刷卡
     * @date: 2016年3月11日 
     * @author: chenxiaolin
     */
    public function actionStatistics()
    {
        $shop_id = isset($_POST['shop_id']) ? $_POST['shop_id'] : '';
        if(empty($shop_id) || intval($shop_id) < 1){
            echo json_encode(array('status' => 1000, 'result' => '参数错误！'));
            exit;
        }
        $shop = SysShop::find()->where('shop_id = :id',[':id' => $shop_id])->one();
        if(empty($shop)){
            echo json_encode(array('status' => 1000, 'result' => '店铺不存在！'));
            exit;
        }
        $obj = new BusMemberConsume();
        //今日
        $today = $obj->statisticsMoney(1, $shop_id);
        //本月
        $month = $obj->statisticsMoney(2, $shop_id);
        $arr = [
            'today' => $today,
            'month' => $month
        ];
        return json_encode(array('status' => 10000, 'result' => $arr));
    }
    /**
     * 线上店铺审核通过后，修改店铺为：线上店铺
     * @date: 2016年3月16日 
     * @author: chenxiaolin
     */
    public function actionUpdateonline(){
        $shop_id = isset($_POST['shop_id']) ? $_POST['shop_id'] : '';
        if(empty($shop_id) || intval($shop_id) < 1){
            echo json_encode(array('status' => 1000, 'result' => ['success' => 0]));
            exit;
        }
        $shop = SysShop::find()->where('shop_id = :id',[':id' => $shop_id])->one();
        if(empty($shop)){
            echo json_encode(array('status' => 1000, 'result' => ['success' => 0]));
            exit;
        }
        $transAction = \Yii::$app->db->beginTransaction();
        try {
            //修改店铺状态
            $shop->isopen_online = 1;
            //修改线上店铺开通数量
            $boss = SysBoss::find()
            ->select(['sys_boss.*','sys_shop.*'])
            ->where("sys_shop.shop_id = ".$shop_id)
            ->leftJoin('sys_shop','sys_boss.boss_uid = sys_shop.boss_uid')->one();
            $boss->online_count = $boss->online_count + 1;
            if($shop->save(false) && $boss->save(false)){
                $transAction->commit();
                return json_encode(array('status' => 10000, 'result' => ['success' => 1]));
            }else{
                echo json_encode(array('status' => 1000, 'result' => ['success' => 0]));
                exit;
            }
        } catch (\Exception $e) {
            $transAction->rollBack();
            echo json_encode(array('status' => 1000, 'result' => ['success' => 0]));
            exit;
        }
    }

    /**
     * 新增美容院预约
     * @date: 2016年3月18日
     * @author: liujunlong
     */
    public function actionServiceorder()
    {
        $model = new BusServiceOrder();
        $model->setAttributes($_POST);

        empty($model->shop_id) && exit( json_encode(['status' => 1000, 'result' => '美容院店铺id不能为空']) );
        empty($model->order_member_name) && exit( json_encode(['status' => 1000, 'result' => '会员名称不能为空']) );
        empty($model->order_member_phone) && exit( json_encode(['status' => 1000, 'result' => '会员电话不能为空']) );
        empty($model->order_time) && exit( json_encode(['status' => 1000, 'result' => '预约日期不能为空']) );
        $model->add_time = date('Y-m-d H:i:s');
        $model->order_item = $model->order_item ?: '';

        if ($model->insert(false)) {
            echo json_encode(['status' => 10000, 'result' => '操作成功']);
        } else {
            echo json_encode(['status' => 1000, 'result' => '操作失败']);
        }
    }
    /**
     * 通过店铺Id获取手机号码
     * @date: 2016年3月21日 
     * @author: chenxiaolin
     */
    public function actionGetmobile(){
        $shop_id = isset($_POST['shop_id']) ? $_POST['shop_id'] : '';
        if(empty($shop_id) || intval($shop_id) < 1){
            echo json_encode(array('status' => 1000, 'result' => '参数错误！'));
            exit;
        }
        $shop = SysShop::find()->where('shop_id = :id',[':id' => $shop_id])->one();
        if(empty($shop)){
            echo json_encode(array('status' => 1000, 'result' => '店铺不存在！'));
            exit;
        }
        $shop_uid = $shop['shop_uid'];
        $boss_uid = $shop['boss_uid'];
        $boss = SysUser::find()->where('uid =:id',[':id' => $boss_uid])->one();
        //老板手机号码
        $boss_mobile = $boss['login_name'];
        if(!empty($shop_uid)){
            $branch = SysUser::find()->where('uid =:id',[':id' => $shop_uid])->one();
            //店长手机号码
            $shop_mobile = $branch['login_name'];
        }else{
           //没有店长 
            $shop_mobile = '';
        }
        $arr = [
            'shop_mobile' => $shop_mobile,
            'boss_mobile' => $boss_mobile
        ];
        return json_encode(array('status' => 10000, 'result' => $arr));
    }
}
